const app = getApp()
const { dataByIdApi, editApi } = require('../../../api/services/yhHidden/index')
const util = require('../../../utils/util.js')

Page({
  data: {
    id: '',
    info: {},
    list: [],
    editComUrl: false,
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      return value
    },
    show: false,
  },

  // 企业签收
  async signInFn() {
    let _this = this

    const params = 'id=' + _this.data.id
    wx.request({
      url: app.globalData.host + 'scy/app/spTrouble/receiveBill',
      method: 'POST',
      data: params, //参数为键值对字符串
      header: {
        //设置参数内容类型为x-www-form-urlencoded
        'content-type': 'application/x-www-form-urlencoded',
        Accept: 'application/json',
        Authorization: 'Bearer ' + wx.getStorageSync('token'),
      },
      success: function (res) {
        console.log(res.data.msg)

        app.toast('签收成功！')
        _this.getDescData()
      },
      error: function (err) {
        let toast = res.data.msg + ''
        app.toast(toast)
      },
    })
  },

  //  企业签名跳转
  signatureFn() {
    wx.navigateTo({
      url: '/pages/safety/signature/signature?state=comUrl',
    })
  },

  // 获取详情
  async getDescData() {
    let params = { id: this.data.id }
    const res = await dataByIdApi(params)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    this.setData({ info: res.data.info, list: res.data.list })
    wx.setStorageSync('isKonw', this.data.info.isKnow)
  },

  // 时间
  handleConfirm(event) {
    this.setData({
      'info.limitDate': util.formatTime(new Date(event.detail)),
      show: false,
    })
  },

  showPopup() {
    this.setData({ show: true })
  },

  // 时间弹窗
  onCloseTime() {
    this.setData({ show: false })
  },

  // 修改隐患
  async handleEditYh() {
    const res = await editApi(this.data.info)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    this.getDescData()
    wx.navigateBack({
      delta: 1, //返回上一级页面
    })
  },

  // 排查项问题点
  handlePoint(event) {
    let { item } = event.currentTarget.dataset
    let itemStr = JSON.stringify(item)
    console.log(item.isOk)
    if (item.isOk == 0) {
      wx.navigateTo({ url: '/pages/company/troubleSpots/troubleSpots?item=' + itemStr })
    } else {
      wx.navigateTo({ url: '/pages/common/point/point?item=' + itemStr })
    }
  },

  onLoad: function (options) {
    this.setData({ id: options.id, statusForm: true })
    this.getDescData()

    if (options.is_ok == 1) {
      this.signInFn() //签收状态1 调用签收
    }
  },

  onShow: function () {
    if (!this.data.editComUrl) {
      this.getDescData()
    }
  },
})
